package io.mqtt.test;

import io.mqttpush.common.events.CommonTopics;
import io.mqttpush.mqttclient.conn.ConnectorOption;
import io.mqttpush.mqttclient.conn.Connetor;
import io.mqttpush.mqttclient.service.DefaultMessageListener;
import io.netty.handler.codec.mqtt.MqttQoS;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RandomSend {


    final static String[] sendContents={"good morning",
            "thank you",
            "sorry to it",
            "welcome",
            "oh,no",
            "is ok",
            "i get it",
            "where is me"};

    final static  SecureRandom secureRandom=new SecureRandom();


    public static void  randomChooseSend(String ip,String id,Integer port,String userId){

        final DefaultMessageListener defaultMessageListener = new DefaultMessageListener();

        ConnectorOption connectorOption =
                ConnectorOption.builder().host(ip).deviceId(id).port(port).username(userId).password("user123456")
                        .connectTimeOut(5000).pingTimeOut(50).build();

        Connetor connetor = new Connetor(connectorOption, defaultMessageListener);


        connetor.subscribeMessage("/root/*", MqttQoS.EXACTLY_ONCE,(byteBuf)->{


            int  fromLen=byteBuf.readInt();

            byte[] bytes=new byte[fromLen];
            byteBuf.readBytes(bytes);
            String from=new String(bytes);




            int topicLen=byteBuf.readInt();
            bytes=new byte[topicLen];
            byteBuf.readBytes(bytes);
            String fromTopic=new String(bytes);

            byteBuf.readInt();


            bytes=new byte[byteBuf.readableBytes()];
            byteBuf.readBytes(bytes);
            System.out.println("recv->"+new String(bytes)+"from->"+from);

            if(id.equals(from)){
                return;
            }
            String sendContent=sendContents[secureRandom.nextInt(sendContents.length)];

            connetor.pubMsg(CommonTopics.ONE_2ONE_CHAT_PREFIX.concat(from),sendContent.getBytes(StandardCharsets.UTF_8),MqttQoS.EXACTLY_ONCE);

        });


        ScheduledExecutorService scheduledExecutorService=new ScheduledThreadPoolExecutor(2);
        scheduledExecutorService.scheduleAtFixedRate(()->{



            int index=secureRandom.nextInt(15);


            //String strIndex=String.format("%02d",index);
            String sendUserId="user".concat(String.valueOf(index));

            String sendContent=sendContents[secureRandom.nextInt(sendContents.length)];

            connetor.pubMsg(CommonTopics.ONE_2ONE_CHAT_PREFIX.concat(sendUserId),sendContent.getBytes(),MqttQoS.EXACTLY_ONCE);

        },1,1, TimeUnit.SECONDS);


        new Thread(){

            public void run(){

                Scanner scanner=new Scanner(System.in);
                String line=null;
                while(!(line=scanner.nextLine()).equalsIgnoreCase("exit")){


                    for (int i = 0; i <20 ; i++) {

                        String sendUserId="user".concat(String.valueOf(i));

                        String sendContent=line;

                        System.out.println(sendContent+"sent-->");

                        connetor.pubMsg(CommonTopics.ONE_2ONE_CHAT_PREFIX.concat(sendUserId),sendContent.getBytes(),MqttQoS.EXACTLY_ONCE);
                    }

                }

            }

        }.start();


    }




    public static void main(String[] args){

        randomChooseSend("47.108.233.67", UUID.randomUUID().toString(),8008,"user99");
    }
}
